package com.bugpool.leilema.tc.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bugpool.leilema.freamwork.exception.APIException;
import com.bugpool.leilema.freamwork.satoken.SaTokenUtil;
import com.bugpool.leilema.freamwork.datascope.DataScopeUtils;
import com.bugpool.leilema.tc.bean.entity.SysUser;
import com.bugpool.leilema.tc.bean.enums.StatusEnum;
import com.bugpool.leilema.tc.bean.req.RoleUserReq;
import com.bugpool.leilema.tc.bean.req.UserReq;
import com.bugpool.leilema.tc.bean.res.UserRes;
import com.bugpool.leilema.tc.mapper.RoleMapper;
import com.bugpool.leilema.tc.mapper.UserMapper;
import com.bugpool.leilema.tc.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author wh
 * @Description
 * @Date 2023/3/6 10:04
 * @Version 1.0
 **/
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;


    @Override
    public Page<UserRes> list(UserReq userReq) {
        Page<UserRes> page = new Page<>(userReq.getCurrent(), userReq.getSize());
        return userMapper.pageList(page, userReq);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(Long userId, String status) {
        if (StatusEnum.getStatusEnum(status) == StatusEnum.DISABLE) {
            //自己不能禁用自己
            if (SaTokenUtil.getUserId().equals(userId)) {
                throw new APIException("用户不能自己禁用自己");
            }
            //判断要禁用的是不是超级管理员
          //  SysUser sysUser = userMapper.selectById(1L);
            if (userId.equals(1L)) {
                throw new APIException("超级管理员不能被禁用");
            }
            //踢人下线
            StpUtil.logout(userId);
        }
        //修改状态
        userMapper.updateStatus(userId, status);
        return true;
    }

    @Override
    public UserRes getUser(Long userId) {
        return userMapper.getUserInfo(userId);
    }

    @Override
    public boolean addUser(SysUser sysUser) {
        userMapper.insert(sysUser);
        return true;
    }

    @Override
    public boolean updateUser(SysUser sysUser) {
        //判断更新的是否是 超级管理员
        if (sysUser.isAdmin()) {
            throw new RuntimeException("超级管理员不能修改");
        }
        userMapper.updateById(sysUser);
        return true;
    }

    @Override
    public boolean updateUserReq(RoleUserReq roleUserReq) {
        return false;
    }

    @Override
    public boolean delUserByIds(List<Long> userIds) {
        //检查是否有 超级管理员的id
        for (Long userId : userIds) {
              if (userId.equals(1)){
                  throw new RuntimeException("超级管理员不可删除");
              }
              //且踢人下线
            StpUtil.logout(userId);
        }
        userMapper.deleteBatchIds(userIds);
        //删除关联的角色表
        roleMapper.delRoleByUserId(userIds);
        return true;
    }
}
